<template>
  <view class="my-page">
    <!-- 标题栏 -->
    <nav-custom></nav-custom>
    <!-- 商品模块 -->
    <view class="cart">
      <!-- 商品列表区域 -->
      <!-- uni-swipe-action 是最外层包裹性质的容器 -->
      <view class="cart-list">
        <uni-swipe-action class="cart-list">
          <block v-for="(item, index) in cartList" :key="item.id">
            <!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过 options 属性来指定操作按钮的配置信息 -->
            <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(item.id)">
              <cart-item :item="item" :cartIndex="index"></cart-item>
            </uni-swipe-action-item>
          </block>
        </uni-swipe-action>
      </view>
      <image v-if="!cartList.length" mode="aspectFit" class="play"
        src="https://xecho.oss-cn-hangzhou.aliyuncs.com/blog/购物车空空如也.png"></image>
      <!-- 底部结算 -->
      <view class="flexd flex justify-between">
        <view class="selectAll">
          <text class="iconfont icon-gouxuan margin-left-15" :class="allInfo.allCheck ? 'color-ffe' : ''"
            @click="handleAllCheck(allInfo.allCheck)"></text>
          <text class="margin-left-5">全选</text>
        </view>
        <text class="f18 color-2c">合计：￥{{ allInfo.allPrice }}</text>
        <view class="submit color-2c" @click="handleOrder">去结算({{ allInfo.checkNum }})</view>
      </view>
    </view>
    <!-- 弹出层 -->
    <cart-overlay></cart-overlay>
    <!-- 登录提示 -->
    <view v-if="loginShow">
      <login-overlay></login-overlay>
    </view>
    <tab-bar tabbar="cart"></tab-bar>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  import CartOverlay from '@/components/cart-overlay.vue'
  import LoginOverlay from '@/components/login-overlay.vue'
  import CartItem from '@/components/cart-item.vue'
  export default {
    components: {
      "cart-overlay": CartOverlay,
      "login-overlay": LoginOverlay,
      "cart-item": CartItem
    },
    data() {
      return {
        index: 0,
        options: [{
          text: '删除', // 显示的文本内容
          style: {
            backgroundColor: 'rgb(240,37,15)' // 按钮的背景颜色
          }
        }]
      }
    },
    onLoad() {
     this.changeLoginShow(false)
    },
    computed: {
      ...mapState({
        cartList: state => state.cart.cartList,
        token: state => state.user.userInfo.sessionToken,
        loginShow: state => state.cart.loginShow
      }),
      ...mapGetters({
        allInfo: 'cart/allInfo'
      })
    },
    methods: {
      ...mapMutations({
        handleAllCheck: 'cart/cartAllCheckMut',
        handleDelCart: 'cart/handleDelCart',
        changeLoginShow: 'cart/changeLoginShow'
      }),
      swipeActionClickHandler(id) {
        this.handleDelCart(id)
      },
      handleOrder() { //结算跳转
        if (!this.cartList.length) {
          uni.showToast({
            title: '购物车内无商品',
            icon: 'none'
          })
        } else if (!this.allInfo.checkNum) {
          uni.showToast({
            title: '请选中需要下单的商品',
            icon: 'none'
          })
        } else if (!this.token) {
          this.changeLoginShow(true)
        } else {
          uni.navigateTo({
            url: '/pagesA/order/order'
          })
        }
      },
    }
  }
</script>


<style lang="scss">
  page {
    background-color: white;
  }

  .cart {
    padding-bottom: 104rpx;

    .cart-list {
      margin-top: 20rpx;
    }

    .play {
      width: 100%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }

  .flexd {
    position: fixed;
    width: 100%;
    height: 100rpx;
    bottom: 124rpx;
    background-color: white;
    line-height: 100rpx;
    /* #ifdef MP-WEIXIN */
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
    box-sizing: content-box;
    /* #endif */
    .submit {
      height: 100%;
      width: 220rpx;
      padding-right: 10rpx;
      background-color: #ffe32a;
      text-align: center;
      font-size: 35rpx;
    }
  }
</style>
